import request from '@/utils/request.js'
// 状态数据
const state = {

	shopInfo: {}, // 商家信息
	commentData: {}, // 评价信息
	menuData: {} // 菜单信息
}
const actions = {
	// 获取分类详情页数据
	async getShopInfo({commit}, id) {
		const res = await request(`/shopInfo/${id}`)
		commit('GET_SHOP_INFO', {
			commentData: res.shopInfo.commentData.data,
			menuData: res.shopInfo.menuData.data
		})
	}
}
const mutations = {
	GET_SHOP_INFO(state, val) {
		state.commentData = val.commentData
		state.menuData = val.menuData
	}
}
const getters = {
	// 商家信息
	shopInfo: state => state.menuData.poi_info,
	// 左侧食品标签
	foods: state => {
		 let foodsList = state.menuData.food_spu_tags
		 if (!foodsList) return []
		 foodsList = foodsList.map(item => {
			 // 菜单左侧每一项的选择数量总和
			 item.count = item.spus.reduce((totalCount, current) => {
				 return totalCount += current.sequence
			 }, 0)
			 // 菜单左侧每一项的选择总价
			 item.totalPrice = item.spus.reduce((total, current) => {
				 return total += current.min_price * current.sequence 
			 }, 0)
			 return item
		 })
		 return foodsList
	},
	// 食品标签对应的商品列表
	spusList: state => {
		return state.menuData.food_spu_tags && state.menuData.food_spu_tags.map(item => item.spus)
	}
}

export default {
	state, actions, mutations, getters
}